﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SMS.DAL.DAHelpers;
using SMS.Entities;

namespace SMS.DAL
{
    public class CityDA : BaseDA<City>
    {
        
        public override City SaveObject(string userName, City tObject)
        {
            #region Procedure
            /*
             *  
create PROCEDURE [dbo].[USP_INS_UPDATE_CITY]    
(@City_ID int,    
@City_Name varchar(50),    
@IsLive bit)    
as    
IF (@City_ID=0)    
BEGIN    
INSERT INTO [Shop].[dbo].[City]     
    (         
     [CityName]  
     ,[IsLive])    
     VALUES    
           (    
     @City_Name,    
     @IsLive )    
     SELECT @@IDENTITY    
END    
ELSE    
BEGIN               
UPDATE [Shop].[dbo].[City]    
   SET [CityName] = @City_Name         
      ,[IsLive] = @IsLive    
 WHERE CityID=@City_ID    
 END   * */
            #endregion
            try
            {
                dbHelper.AddParameter("@City_ID", tObject.CityID);
                dbHelper.AddParameter("@City_Name", tObject.CityName);                
                dbHelper.AddParameter("@IsLive", tObject.IsLive);
                object objID = dbHelper.ExecuteScaler("USP_INS_UPDATE_CITY", System.Data.CommandType.StoredProcedure, System.Data.ConnectionState.Closed);
                if (Convert.ToInt32(objID) > 0)
                    tObject.CityID = Convert.ToInt32(objID);
                LogActivity(userName, string.Format("City Saved with ID {0}", tObject.CityID), tObject);
                return tObject;
            }
            catch (Exception ex)
            {
                LogException(userName, ex, tObject);
                throw new Exception("Error Occured while Saving City");
            }
        }

        public override bool DeleteObject(string userName, City tObject)
        {
            throw new NotImplementedException();
        }

        public override List<City> GetObjectList(string userName, DateTime fromDate, DateTime toDate)
        {
            throw new NotImplementedException();
        }

        public override List<City> SearchObject(string userName, string SearchText)
        {
            #region Procedure
            /*
CREATE PROCEDURE USP_SEARCH_CITY  (@Search_Text varchar(25))  
 as  
 BEGIN  
   
 SELECT [CityID]
      ,[CityName]
      ,[IsLive]
  FROM [shop].[dbo].[City]
  WHERE [CityName] LIKE '%'+ @Search_Text + '%'   
          
 END*/

            #endregion

            try
            {
                dbHelper.AddParameter("@Search_Text", string.IsNullOrWhiteSpace(SearchText) ? string.Empty : SearchText);
                var reader = dbHelper.ExecuteReader("USP_SEARCH_CITY", System.Data.CommandType.StoredProcedure, System.Data.ConnectionState.Closed);
                var list = new List<City>();
                while (reader.Read())
                {
                    list.Add(GetCityObject(reader));
                }
                LogActivity(userName, string.Format("Searched city with text {0}", SearchText), null);
                return list;
            }
            catch (Exception ex)
            {
                LogException(userName, ex, null);
                throw new Exception("Error while searching City");
            }
        }

        private City GetCityObject(System.Data.Common.DbDataReader reader)
        {
            int i = -1;
            City obj = new City();
            if (!reader.IsDBNull(++i))
                obj.CityID = reader.GetInt32(i);
            if (!reader.IsDBNull(++i))
                obj.CityName = reader.GetString(i);
            if (!reader.IsDBNull(++i))
                obj.IsLive = reader.GetBoolean(i);
            return obj;
        }
    }
}
